//
//  BaseView.swift
//  WeiBoText
//
//  Created by 刘世瑛 on 16/1/10.
//  Copyright © 2016年 刘世瑛. All rights reserved.
//

import UIKit
import SnapKit
 protocol BaseViewDelegate:NSObjectProtocol
{
    func  didLoginClick()
    func  didRegistClick()

}

class BaseView: UIView {

    weak var baseDelegate : BaseViewDelegate?
    
    
    
    
    
    
    func   setDataWithString(iconName:String? , tipText:String)
    {
        if let img = iconName{
            
            bigImageView.image = UIImage(named: img)
            
            textlabel.text = tipText
            
            inImageView.hidden = true
           bringSubviewToFront(bigImageView)
        }else{
            
            startAnimating()
        }
    }
    
    
    
    
   func startAnimating(){
    
    let base = CABasicAnimation(keyPath: "transform.rotation")
    base.repeatCount = MAXFLOAT
    base.toValue = 2 * M_PI
    base.duration = 10
    
    
    //让页面失去活性 不移除图层
    base.removedOnCompletion = false

    bigImageView.layer.addAnimation(base, forKey: nil)
    }
   //在构造方法中加载控件
   override init(frame: CGRect) {
    
    super.init(frame: frame)
   
    setUI()
   

    }

    
    //代理方法
 @objc private func touchLogIn()
    {
       baseDelegate?.didLoginClick()
   
        
    }
    
    
   @objc private func touchRegister()
    {
        baseDelegate?.didRegistClick()
        
    }
    
    
    
   required init?(coder aDecoder: NSCoder) {
    
    fatalError("init(coder:) has not been implemented")
  
    
    
    }
    
    
    
    //布局
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setLayOut()
        
        
    }
    
    
    
    
    //更新布局
    
    func setLayOut(){
        bigImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            }
        
        coverView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(regBtn.snp_bottom)
            make.right.left.top.equalTo(self)
        }
        inImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(bigImageView.snp_center)
        }
        
        textlabel.snp_makeConstraints { (make) -> Void in
//            make.left.equalTo(bigImageView.snp_left)
//            make.right.equalTo(bigImageView.snp_right)
            make.width.equalTo(225)
            make.height.equalTo(40)
            make.top.equalTo(bigImageView.snp_bottom).offset(20)
            make.centerX.equalTo(bigImageView)
        }
        logInBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(textlabel.snp_left)
           make.width.equalTo(100)
            make.top.equalTo(textlabel.snp_bottom).offset(20)
        }
        regBtn.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(100)
            make.top.equalTo(textlabel.snp_bottom
                ).offset(20)
            make.right.equalTo(textlabel.snp_right)

        }
    }
    
    
    //搭建界面
    func setUI(){
        
        
        //添加控件
        addSubview(bigImageView)
        addSubview(coverView)
        addSubview(inImageView)
        addSubview(textlabel)
        addSubview(logInBtn)
        addSubview(regBtn)
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
 
        
        
    }
    
    
    
    lazy var bigImageView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    lazy var coverView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    lazy var inImageView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    lazy var textlabel : UILabel = {
        
        let  textl = UILabel()
        textl.font = UIFont.systemFontOfSize(14)
        textl.textAlignment = .Center
        textl.text = "登陆后，别人显示你的评论微博"
        textl.numberOfLines = 0;
        textl.textColor = UIColor.darkGrayColor()
        
        textl.sizeToFit()
        return textl
        
        
    }()
    
    lazy var logInBtn : UIButton = {
        let  logbtn = UIButton()
        logbtn.setTitle("登陆", forState: .Normal)
        logbtn.titleLabel?.font = UIFont.systemFontOfSize(16)
        logbtn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
//        logbtn.setBackgroundImage(UIImage(named:  "common_button_white_disable"), forState: .Normal)
        
        //拉伸图片
        let im = (UIImage(named: "common_button_white_disable"))!
        let w = Int(im.size.width * 0.5)
        let h = Int(im.size.height * 0.5)
        logbtn.setBackgroundImage(im.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
        
        logbtn.addTarget(self, action: "touchLogIn", forControlEvents:.TouchUpInside)

        return logbtn
       
        
    }()
    
    lazy var regBtn : UIButton = {
        
        let  rebtn = UIButton()
        rebtn.setTitle("注册", forState: .Normal)
        rebtn.titleLabel?.font = UIFont.systemFontOfSize(16)
        rebtn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
 
     
        //拉伸图片
        let im = (UIImage(named: "common_button_white_disable"))!
        let w = Int(im.size.width * 0.5)
        let h = Int(im.size.height * 0.5)
        rebtn.setBackgroundImage(im.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
        
        rebtn.addTarget(self, action: "touchRegister", forControlEvents:.TouchUpInside)
        
        return rebtn
        
    }()

}
